var username = null;
var userpwd = null;
var realname = null;
var position = null;
var job = null;
var departmentname = null;
var addBtn = null;
var search = null;
// var backBtn = null;

function priceReg (value){
	value = value.replace(/[^\d.]/g,"");  //清除“数字”和“.”以外的字符
		value = value.replace(/^\./g,"");  //验证第一个字符是数字而不是.
    value = value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的.
    value = value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");//去掉特殊符号￥
	if(value.indexOf(".")>0){
		value = value.substring(0,value.indexOf(".")+3);
	}
	return value;
}


$(function(){
	username = $("#username");
	userpwd = $("#userpwd");
	realname = $("#realname");
	position = $("#position");
	job = $("#job");
	departmentname = $("#departmentname");
	addBtn = $("#add");
	search=$("#search");
	//backBtn = $("#back");
	//初始化的时候，要把所有的提示信息变为：* 以提示必填项，更灵活，不要写在页面上
	username.next().html("*");
	userpwd.next().html("*");
	search.next().html("*");
	position.next().html("*");
	job.next().html("*");
	departmentname.next().html("*");
	//异步获取部门名称下拉列表
	/* $.ajax({
		type:"GET",//请求类型
		url:path+"/jsp/getdepartment.do",//请求的url
		data:{method:"getproviderlist"},//请求参数
		dataType:"json",//ajax接口（请求url）返回的数据类型
		success:function(data){//data：返回数据（json对象）
			if(data != null){
				$("select").html("");//通过标签选择器，得到select标签，适用于页面里只有一个select
				var options = "<option value=\"0\">请选择</option>";
				for(var i = 0; i < data.length; i++){
					//alert(data[i].id);
					//alert(data[i].proName);
					options += "<option value=\""+data[i].id+"\">"+data[i].proName+"</option>";
				}
				$("select").html(options);
			}
		},
		error:function(data){//当访问时候，404，500 等非200的错误状态码
			validateTip(departmentname.next(),{"color":"red"},imgNo+" 获取部门名称列表error",false);
		}
	}); 
		//获取职位类别下拉列表
		$.ajax({
		type:"GET",//请求类型
		url:path+"/jsp/getdepartment.do",//请求的url
		data:{method:"getproviderlist"},//请求参数
		dataType:"json",//ajax接口（请求url）返回的数据类型
		success:function(data){//data：返回数据（json对象）
			if(data != null){
				$("select").html("");//通过标签选择器，得到select标签，适用于页面里只有一个select
				var options = "<option value=\"0\">请选择</option>";
				for(var i = 0; i < data.length; i++){
					//alert(data[i].id);
					//alert(data[i].proName);
					options += "<option value=\""+data[i].id+"\">"+data[i].proName+"</option>";
				}
				$("select").html(options);
			}
		},
		error:function(data){//当访问时候，404，500 等非200的错误状态码
			validateTip(departmentname.next(),{"color":"red"},imgNo+" 获取职务类别列表error",false);
		}
	});
	
	
	
	*/
	/*
	 * 验证
	 * 失焦\获焦
	 * jquery的方法传递
	 */
	username.on("blur",function(){
		if(username.val() != null && username.val() != ""){
			validateTip(username.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(username.next(),{"color":"red"},imgNo+" 用户名不能为空",false);
		}
	}).on("focus",function(){
		//显示友情提示
		validateTip(username.next(),{"color":"#666666"},"* 请输入用户名",false);
	}).focus();
	
	userpwd.on("focus",function(){
		validateTip(userpwd.next(),{"color":"#666666"},"* 请输入密码",false);
	}).on("blur",function(){
		if(userpwd.val() != null && userpwd.val() != ""){
			validateTip(userpwd.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(userpwd.next(),{"color":"red"},imgNo+" 密码不能为空",false);
		}
		
	});
	
	realname.on("focus",function(){
		validateTip(search.next(),{"color":"#666666"},"* 请输入员工姓名",false);
	}).on("blur",function(){
		if(realname.val() != null && realname.val() != ""){
			validateTip(search.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(search.next(),{"color":"red"},imgNo+" 员工姓名不能为空",false);
		}
		
	});
	
	departmentname.on("focus",function(){
		validateTip(departmentname.next(),{"color":"#666666"},"* 请选择部门",false);
	}).on("blur",function(){
		if(departmentname.val() != null && departmentname.val() != "" && departmentname.val() != 0){
			validateTip(departmentname.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(departmentname.next(),{"color":"red"}," 部门不能为空",false);
		}
		
	});
	
	position.on("focus",function(){
		validateTip(position.next(),{"color":"#666666"},"* 请输入职位名称",false);
	}).on("blur",function(){
		if(position.val() != null && position.val() != ""){
			validateTip(position.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(position.next(),{"color":"red"},imgNo+" 职位名称不能为空",false);
		}
		
	});
	
	job.on("focus",function(){
		validateTip(job.next(),{"color":"#666666"},"* 请选择部门",false);
	}).on("blur",function(){
		if(job.val() != null && job.val() != "" && job.val() != 0){
			validateTip(job.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(job.next(),{"color":"red"}," 部门不能为空",false);
		}
		
	});
	
	
	addBtn.on("click",function(){
		if(username.attr("validateStatus") != "true"){
			username.blur();
		}else if(userpwd.attr("validateStatus") != "true"){
			userpwd.blur();
		}else if(realname.attr("validateStatus") != "true"){
			realname.blur();
		}else if(departmentname.attr("validateStatus") != "true"){
			departmentname.blur();
		}else if(position.attr("validateStatus") != "true"){
			position.blur();
		}else if(job.attr("validateStatus") != "true"){
			job.blur();
		}else{
			if(confirm("是否确认提交数据")){
				$("#userform").submit();
			}
		}
	});
	
	/* backBtn.on("click",function(){
		if(referer != undefined 
			&& null != referer 
			&& "" != referer
			&& "null" != referer
			&& referer.length > 4){
		 window.location.href = referer;
		}else{
			history.back(-1);
		}
	}); */
});